package com.weixin.paleituo.activity;

import android.content.Context;
import android.content.Intent;
import android.support.v7.widget.DefaultItemAnimator;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.weixin.paleituo.R;
import com.weixin.paleituo.adapter.ExpressRecycleAdapter;
import com.weixin.paleituo.application.BaseApplication;
import com.weixin.paleituo.base.BasePersonActivity;
import com.weixin.paleituo.bean.ExpressBean;
import com.weixin.paleituo.bean.StatusCode;
import com.weixin.paleituo.bean.TracesBean;
import com.weixin.paleituo.contans.Contans;
import com.weixin.paleituo.http.HttpUtil;
import com.weixin.paleituo.http.ProgressSubscriber;
import com.weixin.paleituo.rxjava.ApiUtils;
import com.weixin.paleituo.util.ToastUtils;
import com.weixin.paleituo.util.UIUtils;
import com.weixin.paleituo.view.LoadingPagerHead;

import java.util.List;

import butterknife.BindView;
import io.reactivex.Observable;

public class ExpressActivity extends BasePersonActivity {


    @BindView(R.id.iv_goodsicon_activity_express)
    ImageView mIvGoodsiconActivityExpress;
    @BindView(R.id.tv_express_company_activity_express)
    TextView mTvExpressCompanyActivityExpress;
    @BindView(R.id.tv_expressnumber_activity_express)
    TextView mTvExpressnumberActivityExpress;
    @BindView(R.id.tv_express_phone)
    TextView mTvExpressPhone;
    @BindView(R.id.recycle_activity_express)
    RecyclerView mRecycleActivityExpress;
    private LoadingPagerHead mLoadingPagerHead;
    private String mOrderDetailId;
    @BindView(R.id.tv_goodnumber_activity_express)
    TextView mtvgoodsNumber;

    @Override
    protected View getContentView(Context context) {
        mLoadingPagerHead = new LoadingPagerHead(this) {
            @Override
            protected void close() {

            }

            @Override
            protected View createSuccessView() {
                return UIUtils.inflate(ExpressActivity.this, R.layout.activity_express);
            }

            @Override
            protected void reLoading() {
                retry();


            }
        };

      //  setContentView(R.layout.activity_express);
        return mLoadingPagerHead;
    }

    /**
     * 重新加载
     */
    private void retry() {

        Observable<StatusCode<ExpressBean>> expressInfoObservable = ApiUtils.getApi().getExpressInfo(mOrderDetailId, BaseApplication.getToken(), BaseApplication.getUserId());
        HttpUtil.getInstance().toSubscribe(expressInfoObservable, new ProgressSubscriber<ExpressBean>(this) {
            @Override
            protected void _onNext(StatusCode<ExpressBean> expressBeanStatusCode) {
                ExpressBean expressBean = expressBeanStatusCode.getData();
                List<TracesBean> traces = expressBean.getTraces();
                if (expressBeanStatusCode.isFlag()&&traces!=null&&traces.size()>0){
                    mLoadingPagerHead.showPagerView(Contans.STATE_SUCCEED);
                    ExpressRecycleAdapter expressRecycleAdapter=new ExpressRecycleAdapter(ExpressActivity.this,traces);
                    mRecycleActivityExpress.setLayoutManager(new LinearLayoutManager(ExpressActivity.this));
                    mRecycleActivityExpress.setItemAnimator(new DefaultItemAnimator());
                    mRecycleActivityExpress.setAdapter(expressRecycleAdapter);

                    //
                    UIUtils.loadImageView(ExpressActivity.this,expressBean.getListUrl(),mIvGoodsiconActivityExpress);
                    mTvExpressCompanyActivityExpress.setText("快递公司     "+expressBean.getExpressCompany());
                    mTvExpressnumberActivityExpress.setText("快递单号    "+expressBean.getLogisticCode());
                    mTvExpressPhone.setText("店铺电话    "+expressBean.getStoreTelephone());

                    mtvgoodsNumber.setText(expressBean.getGoodsnum()+"件商品");

                }else {
                    mLoadingPagerHead.showPagerView(Contans.STATE_EMPTY);
                }




            }
            @Override
            protected void _onError(String message) {
                ToastUtils.makeText(message);
                mLoadingPagerHead.showPagerView(Contans.STATE_ERROR);




            }
        },"",lifecycleSubject,false,true);


    }

    @Override
    protected void initData() {
        super.initData();
        mTvTitle.setText("查看物流");
        mViewline.setVisibility(View.VISIBLE);

        Intent intent = getIntent();
        mOrderDetailId = intent.getStringExtra(Contans.INTENT_DATA);
        retry();


    }



public static void startActivity(Context context ,String orderDetailId){
        Intent intent=new Intent(context,ExpressActivity.class);
        intent.putExtra(Contans.INTENT_DATA,orderDetailId);
        context.startActivity(intent);
}


}
